package net.helpfulwebservices.cogitologopedia.ws;

import net.helpfulwebservices.cogitologopedia.Activity_login;
import net.helpfulwebservices.cogitologopedia.Activity_option;

import net.helpfulwebservices.cogitologopedia.R;
import net.helpfulwebservices.cogitologopedia.utility.GUI;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.PropertyInfo;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;

import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.util.Log;
import android.widget.Button;


public class wsConnectionTask extends AsyncTask<String, Integer, wsResponse>
{
	private Context _context;
	private Activity_login _activity_login;
	private int _statusWS;
	

	
	
	/**
	 * @return the _statusWS
	 */
	public int getStatusWS() {
		return _statusWS;
	}


	/**
	 * @param _statusWS the _statusWS to set
	 */
	public void setStatusWS(int _statusWS) {
		this._statusWS = _statusWS;
	}


	public wsConnectionTask(Context context, Activity_login a) {
        this._context = context;
        this._activity_login=a;
    }

	
	@Override
	protected void onPostExecute(wsResponse result) {
		 super.onPostExecute(result);

		 Button buttonLogin = (Button) this._activity_login.findViewById(R.id.bttLogin);
		 Button buttonbttClear = (Button) this._activity_login.findViewById(R.id.bttClear);
		 
		 
     	if (result.getAccount().compareTo("null")==0){
    		//this._activity_login.messageBox("Accesso", "Le credenziali inserite non sono valide.",R.drawable.warning);
    		GUI.messageBox(this._activity_login,"Accesso", "Le credenziali inserite non sono valide.",R.drawable.warning);
    		this._activity_login.setStatusWs(0);
    		
        	buttonLogin.setEnabled(true);
        	buttonbttClear.setEnabled(true);
    	}else if (result.getTicket()<=0){
    		this._activity_login.setStatusWs(-1);
    		GUI.messageBox(this._activity_login,"Info", "Nessun ticket disponibile.\nContatta l'amministratore.",R.drawable.warning);

        	buttonLogin.setEnabled(true);
        	buttonbttClear.setEnabled(true);
    	}else{
    		this._activity_login.setStatusWs(1);
    		Intent intent = new Intent(this._context,Activity_option.class);
    		this._context.startActivity(intent); 	
    	}
     	
     	Log.v("wsConnectionTask","onPostExecute:"+result.toString());
	}

	@Override
    protected wsResponse doInBackground(String...params) 
    {
		
		wsResponse _wsResponse = null;
		
		String txtUsername=params[0];
		String txtPassword=params[1];
		String ws_namespace=params[2];
		String ws_method=params[3];
		String ws_url=params[4];
		String ws_soap_action_accountinfo=params[5];
		
    	Log.i("wsConnectionTask", "doInBackground..." + 
    			txtUsername + ", " + 
    			txtPassword + ", " +
    			ws_namespace + ", " +
    			ws_method + ", " +
    			ws_url + ", "+
    			ws_soap_action_accountinfo);
    	
    	try
    	{
    		
    		SoapObject request = new SoapObject(
    				ws_namespace, 
    				ws_method);
    		
	        PropertyInfo account =new PropertyInfo(); 
	        account.setName("account"); 
	        account.setValue(txtUsername); 
	        account.setType(PropertyInfo.STRING_CLASS); 
	        request.addProperty(account);         
	

	        PropertyInfo password =new PropertyInfo(); 
	        password.setName("password"); 
	        password.setValue(txtPassword); 
	        password.setType(PropertyInfo.STRING_CLASS); 
	        request.addProperty(password);         
	
	        
	        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
	        envelope.dotNet=true;
	        envelope.setOutputSoapObject(request);
	        
	        
	        HttpTransportSE httpTransport = new HttpTransportSE(ws_url);
	        httpTransport.setXmlVersionTag("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"); 
	        httpTransport.debug=true;
	        

	        
	        try {         
	        	httpTransport.call(ws_soap_action_accountinfo, envelope);
	        	SoapObject response = (SoapObject) envelope.getResponse();
	        	Log.v("wsConnectionTask","SOAP response:\n\n "+httpTransport.responseDump);
	        	
	        	
	        	int check =Integer.parseInt(response.getPrimitivePropertyAsString("check"));
	        	int ticket =Integer.parseInt(response.getPrimitivePropertyAsString("ticket"));
	        	int id_user =Integer.parseInt(response.getPrimitivePropertyAsString("id_user"));
	        	this.setStatusWS(id_user);
	        	
	        	_wsResponse= new wsResponse(id_user, 
	        			response.getPrimitivePropertyAsString("account"), 
	        			response.getPrimitivePropertyAsString("nome"), 
	        			response.getPrimitivePropertyAsString("email"), ticket, check);
	        	
	        	
	        	
	        } catch (Exception ee) {   
	        	Log.e("wsConnectionTask", ee.toString());   
	        }         
    	} catch (Exception ee) {       
        	Log.e("wsConnectionTask", ee.toString());
        }         

    	
    	return _wsResponse;
	}
}